package com.yanqu.road.server.channelserver.manager.activity.entername;

import com.yanqu.road.entity.activity.entername.*;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class EnterNameGroupCtrl {
    //1v1  直接使用 zoneId 当分组id
    public void grouping(EnterNameModel nameModel){

        List<ChannelFightMatchRule> ruleList = nameModel.getRuleList();

        List<ChannelFightZone> zoneList = nameModel.getZoneList();

        if(ruleList != null && zoneList != null && ruleList.size() > 0 && zoneList.size() > 0){
            int activityId = nameModel.getActivityInfo().getActivityId();
            Object object = ChannelEnterNameMgr.getActivityLock(activityId);
            synchronized (object){
                if(nameModel.getMatchMap().size() == 0){

                    Map<Long, UserEnterName> nameMap = nameModel.getNameMap();

                    if(nameMap != null && nameMap.size() > 0){

                        //统计人数
                        Map<Integer, EnterNameCount> countMap = new HashMap<>();
                        for (Map.Entry<Long, UserEnterName> entry : nameMap.entrySet()) {
                            UserEnterName userEnterName = entry.getValue();
                            int zoneId = userEnterName.getZoneId();
                            countMap.putIfAbsent(zoneId, new EnterNameCount(zoneId, 0));
                            long oldCount = countMap.get(zoneId).getCount();
                            countMap.get(zoneId).setCount(oldCount + 1);
                            countMap.get(zoneId).addZoneId(userEnterName.getZoneId());
                        }

                        //沒有报名的地区，默认人数0
                        for (ChannelFightZone fightZone : nameModel.getZoneList()) {
                            if(!countMap.containsKey(fightZone.getAreaId())){
                                countMap.put(fightZone.getAreaId(), new EnterNameCount(fightZone.getAreaId(), 0));
                                countMap.get(fightZone.getAreaId()).addZoneId(fightZone.getAreaId());
                            }
                        }

                        List<EnterNameCount> nameList = new ArrayList<>(countMap.values());
                        Collections.sort(nameList);
                        for (EnterNameCount nameCount : nameList) {
                            ChannelEnterNameMgr.getLogger().info("grouping activityId {}, groupId {}, count {}", activityId, nameCount.getGroupId(), nameCount.getCount());
                        }

                        Random random = new Random();
                        //分组 1v1
                        Map<Integer, ActivityGroupMatch> tempGroupMatch = new ConcurrentHashMap<>();
                        for (ChannelFightMatchRule matchRule : ruleList) {
                            List<Integer> rankList = StringUtils.stringToIntegerList(matchRule.getRankRange(), "\\|");
                            int start = rankList.get(0);
                            int end = rankList.get(1);
                            List<EnterNameCount> poolList = new ArrayList<>();
                            for (int i = start - 1; i < end && i < nameList.size(); i++) {
                                poolList.add(nameList.get(i));
                            }
                            while (poolList.size() > 0){
                                int idxOne = random.nextInt(poolList.size());
                                EnterNameCount one = poolList.remove(idxOne);
                                EnterNameCount two = null;
                                if(poolList.size() == 0){

                                }else if(poolList.size() == 1){
                                    two = poolList.remove(0);
                                }else {
                                    two = poolList.remove(random.nextInt(poolList.size()));
                                }
                                ActivityGroupMatch groupOne = new ActivityGroupMatch();
                                groupOne.setActivityId(activityId);
                                groupOne.setGroupId(one.getGroupId());
                                groupOne.setZoneIdList(one.getZoneList());

                                if(two != null) {
                                    ActivityGroupMatch groupTwo = new ActivityGroupMatch();
                                    groupTwo.setActivityId(activityId);
                                    groupTwo.setGroupId(two.getGroupId());
                                    groupTwo.setZoneIdList(two.getZoneList());
                                    groupTwo.setEnemyGroupId(groupOne.getGroupId());
                                    groupTwo.setInsertOption();
                                    tempGroupMatch.put(groupTwo.getGroupId(), groupTwo);

                                    groupOne.setEnemyGroupId(groupTwo.getGroupId());
                                }
                                groupOne.setInsertOption();
                                tempGroupMatch.put(groupOne.getGroupId(), groupOne);
                                ChannelEnterNameMgr.getLogger().info("activityId {}, groupId {}, match groupId {}", activityId, groupOne.getGroupId(), groupOne.getEnemyGroupId());
                            }
                        }

                        if(tempGroupMatch.size() > 0){
                            nameModel.setMatchMap(tempGroupMatch);

                            //更新报名数据
                            for (Map.Entry<Long, UserEnterName> entry : nameMap.entrySet()) {
                                UserEnterName userEnterName = entry.getValue();
                                int zoneId = userEnterName.getZoneId();
                                userEnterName.setGroupId(nameModel.getGroupId(zoneId));
                            }
                        }
                    }
                }
            }
        }

    }

}
